/** 
 * 实现K-V存取
 * var myMap = new Map();
 * myMap.put("key","value");
 * var key = myMap.get("key");
 * myMap.remove("key");
 */
function Map(){this.elements = new Array();this.size = function(){return this.elements.length;};
	this.isEmpty = function(){return (this.elements.length < 1);};
	this.clear = function(){this.elements = new Array();};
	this.put = function(_key, _value){this.remove(_key);this.elements.push({key: _key, value: _value});};
	this.remove = function(_key){try {for (i = 0; i < this.elements.length; i++) {if (this.elements[i].key == _key) {this.elements.splice(i, 1);return true;}}} catch (e) {return false;}return false;};
	this.get = function(_key){
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].key == _key) { return this.elements[i].value; }
			}
		} catch (e) {
			return null;
		}
		return null;
	};
	
	this.element = function(_index){
		if (_index < 0 || _index >= this.elements.length) { return null; }
		return this.elements[_index];
	};
	
	this.containsKey = function(_key){
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].key == _key) {
					return true;
				}
			}
		} catch (e) {
			return false;
		}
		return false;
	};
	
	this.values = function(){
		var arr = new Array();
		for (i = 0; i < this.elements.length; i++) {
			arr.push(this.elements[i].value);
		}
		return arr;
	};
	
	this.keys = function(){
		var arr = new Array();
		for (i = 0; i < this.elements.length; i++) {
			arr.push(this.elements[i].key);
		}
		return arr;
	};
}


/**
 * json 转化成String
 * @param obj
 * @returns
 */
var jsonToString =  function(obj){  
    var THIS = this;   
    switch(typeof(obj)){  
        case 'string':  
            return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';  
        case 'array':  
            return '[' + obj.map(THIS.jsonToString).join(',') + ']';  
        case 'object':  
             if(obj instanceof Array){  
                var strArr = [];  
                var len = obj.length;  
                for(var i=0; i<len; i++){  
                    strArr.push(THIS.jsonToString(obj[i]));  
                }  
                return '[' + strArr.join(',') + ']';  
            }else if(obj==null){  
                return 'null';  
  
                }else{  
                    var string = [];  
                    for (var property in obj) string.push(THIS.jsonToString(property) + ':' + THIS.jsonToString(obj[property]));  
                return '{' + string.join(',') + '}';  
            }  
        case 'number':  
            return obj;  
        case false:  
            return obj;  
    }  
};

function ltrim(str){
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1){
        var j=0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1){
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}

function rtrim(str){
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1){
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1){
            i--;
        }
        s = s.substring(0, i+1);
    }
    return s;
}
 
function trim(str){
    return rtrim(ltrim(str));
}

function is_empty(str){
    return trim(str)=="";
}

/**
 * 转化成json对象
 * @param data
 * @returns
 */
function json(data){
	return eval('(' + data + ')');
}

/**
 * alert
 */
var dAlert = function (msg){
	$.ligerDialog.warn(msg);
};

var flexigridWidth = function (){
	var l_tab_content_width = $.cookie("l-tab-content-width");
	if(l_tab_content_width == null){l_tab_content_width = $(window.parent.document).find(".l-tab-content").width();}
	return (l_tab_content_width -2);
};
var flexigridHeight = function (fg){
	var fg_obj ;
	if(!fg)fg_obj = $("#dataTable");else fg_obj =fg;
	var markHeight=0;
	var exp = fg_obj.parent().children().not(fg_obj);
	exp.each(function(){if($(this).css("display")!='none')markHeight += $(this).height();});
	var l_tab_content_height = $.cookie("l-tab-content-height");
	if(l_tab_content_height == null){l_tab_content_height = $(window.parent.document).find(".l-tab-content").height();}
	return (l_tab_content_height- markHeight-53);
};

/**
 * 打开模态窗口
 * @param options
 * @returns
 */
var winMap =new Map();
var dWindow = function (options){
	var win;
	if(options.url)win = $.ligerDialog.open({ title:options.title,height: options.height, url: options.url, width: options.width,timeParmName:true });
	else if(options.target){var _div =winMap.get(options.target);if(_div==null){_div = $("#"+options.target); winMap.put(options.target,_div);}win = $.ligerDialog.open({ title:options.title,height: options.height, target:_div, width: options.width});_div.show();}
	return win;
	};
	

function remove_id(elem_id){
	$('#' + elem_id).remove();
}

function slide_up(elem_id){
	$('#' + elem_id).slideUp();
}

function toggle(elem_id){
	$('#' + elem_id).toggle();
}

/**
 * 使用ajax提交数据
 */
function ajax_post(the_url,the_param,succ_callback){
	$.ajax({
		type:'POST',
		url:the_url,
		data:the_param,
		success:succ_callback,
		error:function(html){
			alert("提交数据失败，代码:" +html.status+ "，请稍候再试");
		}
	});
}
/**
 * ajax提交,alert返回信息
 * @param the_url
 * @param the_param
 */
function ajax_call(the_url,the_param){
	$.ajax({
		type:'POST',
		url:the_url,
		data:the_param,
		success:function(html){alert(html);},
		error:function(html){
			alert("提交数据失败，代码:" +html.status+ "，请稍候再试");
		}
	});
}
/**
 * ajax提交并刷新当前页面
 * @param the_url
 * @param the_param
 */
function ajax_call_and_refresh(the_url, the_param){
	$.ajax({
		type:'POST',
		url:the_url,
		data:the_param,
		success:function(html){location.reload();},
		error:function(html){
			alert("提交数据失败，代码:" +html.status+ "，请稍候再试");
		}
	});
}



/**
 * 控件封装方法---start
 */

/**
 * 判断是否安装客户端
 */
function isInstallClient(){
	try{
        var mKeySN = SMObj.WebGetKeySN();
        if(is_empty(mKeySN)){
        	return false;
        }else return true;
    }catch(e){
        alert("请先在该机器上安装相应<签章软件>");
        return false;
	}
}
//判断是否绑定key,如果绑定
function haveRegisterKey(){
	ajax_post("login.action", the_param, succ_callback);

	
	SMObj.WebMsgTextClear();
	SMObj.SetMsgByName("OPTION","HAVEREGISTERKEY");
    if(!SMObj.WebSendMessage()){
    	return false;
	}
	radmon = SMObj.GetMsgByName("radmon");
	if(radmon == ""){
		return false;
	}
	try{
        var mKeySN = iSignaturePortal.WebGetKeySn();
        if(mKeySN == "" || mKeySN == null){
        	alert("请先注册客户端控件！");
	    }
    }catch(e){
        alert("请先注册客户端控件！");
        return false;
	}

	return true;
}



function verifyKeyPWD(pwd){
	try{
		var username = iSignaturePortal.WebGetUserName(pwd);
		if(username == null || username == ""){
			return false;
		}
	}catch(e){
		return false;
	}
	var rand3des = iSignaturePortal.Web3DesCrypt(radmon,pwd);
	
	ajax_post("login.action", the_param, succ_callback);
	
	SMObj.WebMsgTextClear();
	SMObj.SetMsgByName("OPTION","VERIFYKEYPWD");
	SMObj.SetMsgByName("USERNAME", username);
	SMObj.SetMsgByName("RAND3DES", rand3des);
	SMObj.SetMsgByName("KEYSN", SMObj.WebGetKeySN());
	SMObj.SetMsgByName("RADMON", radmon);
    if(!SMObj.WebSendMessage()){
    	alert("该管理员密钥盘未绑定，无权登录!");
    	return false;
	}
	return true;
}
/**
 * 控件封装方法---end
 */



